use sea_orm::{ConnectOptions, DatabaseConnection};
use state::Storage;

#[cfg(test)]
static DB: state::Storage<sea_orm::DatabaseConnection> = Storage::new();

#[cfg(test)]
pub(crate) async fn get_connection() -> &'static DatabaseConnection {
    if let Some(db) = DB.try_get() {
        db
    } else {
        dotenv::dotenv().ok();
        let db_url = dotenv::var("DATABASE_URL").expect("cannot connect to db");

        let opt = ConnectOptions::new(db_url);

        let conn = sea_orm::Database::connect(opt)
            .await
            .expect("连接到数据库失败");

        DB.get_or_set(move || conn)
    }
}
